package com.zbkj.service.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.Vehicle;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.VehicleRequest;
import com.zbkj.common.request.VehicleSearchRequest;
import com.zbkj.service.dao.VehicleDao;
import com.zbkj.service.service.VehicleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * @author keNan
 * @description VehicleServiceImpl 接口实现
 * @date 2025-06-25
 */
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleDao, Vehicle> implements VehicleService {

    @Resource
    private VehicleDao dao;


    /**
     * 列表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<Vehicle>
     * @author keNan
     * @since 2025-06-25
     */
    @Override
    public List<Vehicle> getList(VehicleSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<Vehicle> list = dao.getListByVehicleSearchRequest(request);
        return list;
    }


    /**
     * 新增车辆信息
     *
     * @param vehicleRequest
     * @return
     */
    @Override
    public Boolean saveVehicle(VehicleRequest vehicleRequest) {
        LambdaQueryWrapper<Vehicle> lqw = Wrappers.lambdaQuery();
        lqw.eq(Vehicle::getDeliveryPersonnelId, vehicleRequest.getDeliveryPersonnelId());
        lqw.eq(Vehicle::getIsDel, 0);
        int count = count(lqw);
        if (count > 0) {
            throw new CrmebException("该配送专员已绑定车辆了,请勿重复绑定");
        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleRequest, vehicle);
        vehicle.setCreateTime(new Date());
        return save(vehicle);
    }

}

